/*
 * Copyright (c) 2020-2099 the original author or authors. All rights reserve.
 */
package org.tianyun.cloud.common.dto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.tianyun.cloud.common.constants.ResponseCode;

/**
 * 标准传输对象
 *
 * @auther ebert_chan
 */
@ApiModel
public class RestResult<T> extends BaseDTO {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "响应状态码，200表示成功，其他值请查阅异常码说明", required = true)
    private String code;

    @ApiModelProperty(value = "响应消息", required = true)
    private String message;

    @ApiModelProperty(value = "响应数据")
    private T data;

    @ApiModelProperty(value = "服务器当前时间戳(毫秒)", required = true)
    private Long timestamp;

    private RestResult() {
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    public static class RestResultBuilder<T> {

        private RestResult<T> result;

        public RestResultBuilder() {
        }

        public static <T> RestResultBuilder<T> instance() {
            RestResultBuilder<T> builder = new RestResultBuilder<T>();
            builder.result = new RestResult<>();
            return builder;
        }

        private RestResultBuilder<T> result(String code, String message, T data) {
            this.result.setCode(code);
            this.result.setMessage(message);
            this.result.setData(data);
            this.result.setTimestamp(System.currentTimeMillis());
            return this;
        }

        public RestResultBuilder<T> success(T data) {
            return this.result(ResponseCode.SUCCESS, null, data);
        }

        public RestResultBuilder<T> success(String message, T data) {
            return this.result(ResponseCode.SUCCESS, message, data);
        }

        public RestResultBuilder<T> failure(String code, String message) {
            return this.failure(code, message, null);
        }

        public RestResultBuilder<T> failure(String code, String message, T data) {
            return this.result(code, message, data);
        }

        public RestResult<T> build() {
            return this.result;
        }

    }

}
